<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item> 会员充值 </el-breadcrumb-item>
        <el-breadcrumb-item>兑换配置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>
		<template slot-scope="scope" slot="menuLeft">
		  <el-tag type="danger" ><b> *支持版本向上兼容</b></el-tag>
		</template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appConfigExchangeList as baseURL,selectVersion} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "sort&ASC,create_time&ASC",
        consume_type: 1
      },
      option: {
        border: true,
        selection: true,
        border: true,
        align: "center",
        headerAlign: "center",
        column: [
          {
            label: "版本号",
            prop: "app_version",
            type: "input",
            search: true,
            placeholder: "列入: 1.0.0",
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "消耗Bonus",
            prop: "consume_number",
            type: "number",
            span:13,
            rules: [
              {
                required: true,
                message: "消耗Bonus必须填写",
              },
            ],
          },
          {
            label: "兑换类型",
            prop: "exchange_type",
            type: "select",
            span:13,
            placeholder: "请输入兑换类型",
            dicData:[{label:' Coins',value:0},{label:'Vip',value:2}],
            rules: [
              {
                required: true,
                message: "兑换类型必须填写",
              },
            ],
          },
          {
            label: "兑换数量",
            prop: "exchange_number",
            type: "number",
            span:13,
            rules: [
              {
                required: true,
                message: "兑换数量必须填写",
              },
            ],
          },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            span:12,
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.pageAndParam}`,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      row.create_time = new Date();
      row.consume_type = 1;
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.edit_time = new Date();
      let data = await common(`${baseURL.save}`, row, null, "POST");
      if (data == null) {
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.edit_time = new Date();
      row.consume_type = 1;
      let data = await common(`${baseURL.update}`, row, null, "POST");
      if (data == null) {
        return;
      }

      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            `${baseURL.delete}`,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      // params.title = `%${params.title ? params.title : ""}%`;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>